package com.biz.primus.ms.member.service;

import com.biz.primus.model.member.exception.UserException;
import com.biz.primus.model.member.exception.UserExceptionType;
import com.biz.primus.model.member.vo.MemberFootprintVo;
import com.biz.primus.model.member.vo.PageVo;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.member.dao.po.MemberFootprint;
import com.biz.primus.ms.member.dao.redis.MemberFootprintRedisDao;
import com.biz.primus.ms.member.dao.repository.MemberFootprintRepository;
import com.biz.primus.ms.member.trans.MemberFootprintToMemberFootprintRo;
import com.biz.primus.ms.member.trans.MemberFootprintToMemberFootprintVo;
import com.biz.primus.ms.member.trans.MemberFootprintVoToMemberFootprint;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * 足迹
 * @author jun.liu
 * @date 2017年10月23日
 * @reviewer
 * @see
 */
@Slf4j
@Service
public class MemberFootprintService extends AbstractBaseService
{
    private final MemberFootprintRedisDao memberFootprintRedisDao;

    private final MemberFootprintRepository memberFootprintRepository;

    @Autowired
    public MemberFootprintService(MemberFootprintRedisDao memberFootprintRedisDao, MemberFootprintRepository memberFootprintRepository)
    {
        this.memberFootprintRedisDao = memberFootprintRedisDao;
        this.memberFootprintRepository = memberFootprintRepository;
    }

    /**
     * 增加一次足迹
     * @author jun.liu
     * @param vo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void addMemberFootprint(MemberFootprintVo vo)
    {
        if (vo == null)
        {
            throw new UserException(UserExceptionType.PARAMS_ERROR);
        }
        MemberFootprint po = new MemberFootprintVoToMemberFootprint().apply(vo);
        po.setClean(false);
        saveOrUpdateUsingPoPKIncreament(memberFootprintRepository, memberFootprintRedisDao, po, new MemberFootprintToMemberFootprintRo());
    }

    /**
     * 分页查询
     * @param memberId
     * @param page
     * @param size
     * @return
     */
    public PageVo<MemberFootprintVo> findPageMemberFootprint(Long memberId, Integer page, Integer size)
    {
        if (memberId == 0 || memberId == null) {
            throw  new UserException(UserExceptionType.MEMBERID_NULL);
        }
        PageVo vo = new PageVo();
        Page<MemberFootprint> returnPage = memberFootprintRepository.findByMemberIdAndCleanOrderByUpdateTimestampDesc(memberId, false, new PageRequest(page, size));
        if(returnPage != null)
        {
            if (returnPage.getContent() != null && !returnPage.getContent().isEmpty())
            {
                vo.setItems(Lists.transform(returnPage.getContent(), new MemberFootprintToMemberFootprintVo()));
            }
            vo.setTotal(returnPage.getTotalElements());
            vo.setPage(page);
        }
        return vo;
    }

    /**
     * 清楚会员足迹
     * @author jun.liu
     * @param memberId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void cleanMemberFootprint(Long memberId)
    {
        if (memberId == null|| memberId == 0L)
        {
            throw new UserException(UserExceptionType.MEMBERID_NULL);
        }
        List<MemberFootprint> pos = this.memberFootprintRepository.findByMemberIdAndClean(memberId, false);
        if(pos == null)
        {
            throw new UserException(UserExceptionType.FOOTPRINT_NULL);
        }
        for(MemberFootprint po : pos)
        {
            po.setClean(true);
            po.setUpdateTimestamp(new Timestamp(System.currentTimeMillis()));
        }
        batchSaveOrUpdateUsingPoPKIncreament(memberFootprintRepository, memberFootprintRedisDao, pos, new MemberFootprintToMemberFootprintRo());
    }


}
